﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OestsCommon.Model;
using Utils;

namespace OestsDataBase
{
    public class SubjectChoiceDAL
    {

        private static Choice ReadData(DataRow dr)
        {
            Choice choice = new Choice();
            choice.Id = Int32.Parse(dr["id"].ToString());

            choice.MajorId = dr["majorId"].Equals(DBNull.Value) ? 0 : int.Parse(dr["majorId"].ToString());
            choice.PostId = dr["postId"].Equals(DBNull.Value) ? 0 : int.Parse(dr["postId"].ToString());
           // choice.Level = (DifficultyLevel)(dr["difficultylevel"].Equals(DBNull.Value) ? 0 : Int32.Parse(dr["difficultylevel"].ToString()));

            choice.Title = dr["title"].ToString();
            choice.A = dr["a"].ToString();
            choice.B = dr["b"].ToString();
            choice.C = dr["c"].ToString();
            choice.D = dr["d"].ToString();

            choice.E = dr["e"].Equals(DBNull.Value) ? null : dr["e"].ToString();
            string corrects = dr["corrects"].ToString();
            choice.Correct = corrects.ToStrList();

            choice.DefaultScore = dr["defaultscore"].Equals(DBNull.Value) ? 0 : int.Parse(dr["defaultscore"].ToString());
            choice.ProjectId = dr["projectId"].Equals(DBNull.Value) ? 0 : int.Parse(dr["projectId"].ToString());
            return choice;
        }

        /// <summary>
        /// 返回 Identity
        /// </summary>
        /// <param name="choice"></param>
        /// <returns></returns>
        public static int AddChoice(Choice choice)
        {
            string corrects = choice.Correct.StrListToString();
            string strSql = string.Format("insert into T_Subject_Choice (majorId, postId,title, a, b, c,d,e,corrects,difficultylevel,projectId,defaultscore) " +
                                                             "values({0},  {1},  '{2}', '{3}','{4}','{5}','{6}','{7}','{8}',{9},{10},{11});SELECT @@Identity",
                choice.MajorId, choice.PostId, choice.Title, choice.A, choice.B, choice.C, choice.D, choice.E, corrects,/* (int)choice.Level,*/ choice.ProjectId,choice.DefaultScore);
            object obj = SqlHelper.ExecuteScalar(strSql);
            return Int32.Parse(obj.ToString());
        }

        public static List<Choice> GetData()
        {
            List<Choice> list = new List<Choice>();
            string strSql = "select * from T_Subject_Choice ";
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null) { return null; }
            foreach (DataRow dr in dt.Rows)
            {
                Choice choice = ReadData(dr);
                list.Add(choice);
            }
            return list;
        }

        public static bool Delete(int choiceId)
        {
            string strSql = string.Format("delete from T_Subject_Choice where id = @id");
            int n = SqlHelper.ExecuteNonQuery(strSql, new SqlParameter("@id", choiceId));
            if (n != 0)
            {
                return true;
            }
            return false;
        }

        public static bool Modify(Choice choice)
        {
            string corrects = choice.Correct.StrListToString();
            string strSql = string.Format("update T_Subject_Choice set majorId={0},postId={1},title='{2}',a='{3}',b='{4}',c='{5}',d='{6}',e='{7}',corrects='{8}',difficultylevel={9},projectId={10} where id = @id",
                choice.MajorId, choice.PostId, choice.Title, choice.A, choice.B, choice.C, choice.D, choice.E, corrects,/* (int)choice.Level,*/ choice.ProjectId);
            int n = SqlHelper.ExecuteNonQuery(strSql, new SqlParameter("@id", choice.Id));
            if (n != 0)
            {
                return true;
            }
            return false;
        }


        public static Choice GetChoiceById(int id)
        {
            string strSql = "select * from T_Subject_Choice where id = @id ";
            DataTable dt = SqlHelper.ExecuteDataTable(strSql, new SqlParameter("@id", id));
            if (dt == null) { return null; }
            if (dt.Rows.Count == 0)
            {
                Exception e = new Exception(string.Format("在选择题库中找不到 id 为 “{0}” 的选择题", id.ToString()));
                throw e;
            }
            DataRow dr = dt.Rows[0];
            Choice choice = ReadData(dr);
            return choice;
        }

        public static bool CheckExist(int id)
        {
            string strSql = "select Count(*) from T_Subject_Choice where id = @id";
            object obj = SqlHelper.ExecuteScalar(strSql);
            return ((int)obj > 0); ;
        }
    }
}
